package com.lg.mengbase.testlistview.adapter;

import android.content.Context;
import android.view.View;
import android.widget.AbsListView;

import com.lg.baselibrary.adapter.BaseAbsListViewAdapter;
import com.lg.baselibrary.adapter.BaseAbsListViewHolder;
import com.lg.baselibrary.adapter.BaseViewHolder;
import com.lg.baselibrary.dialog.AlertDialog;
import com.lg.mengbase.R;
import com.lg.mengbase.entity.ItemData;

/**
 * Created by LiGang on 2017/5/28.
 */

public class MyAdapter extends BaseAbsListViewAdapter<ItemData> implements BaseViewHolder.OnClickListener {
    private AbsListView absListView;
    public MyAdapter(Context context,AbsListView absListView) {
        super(context);
        this.absListView = absListView;
    }

    @Override
    public void convert(BaseAbsListViewHolder viewHolder, ItemData item, int position) {
        viewHolder.setText(R.id.tv_name,item.getName())
                .setImageResource(R.id.img_head,item.getImgId());
        viewHolder.setOnClickListener(R.id.btn_delete,this);
        viewHolder.setOnClickListener(R.id.btn_update,this);
    }

    @Override
    protected int getItemViewLayout(int position) {
        return R.layout.item;
    }
    private AlertDialog dialog;
    private int deletePosition;
    @Override
    public void onClick(View view, int position) {
        deletePosition = position;
        if(view.getId() == R.id.btn_delete){
            if(dialog == null){
                dialog = new AlertDialog.Builder(getContext())
                        .setContentView(R.layout.dialog_delete)
                        .create();
                dialog.setText(R.id.tv_dialog_title,"删除数据");
                View.OnClickListener onClickListener = new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (v.getId()){
                            case R.id.tv_dialog_ok:
                                removeData(deletePosition);
                                break;
                        }
                        dialog.dismiss();
                    }
                };
                dialog.findViewById(R.id.tv_dialog_cancle).setOnClickListener(onClickListener);
                dialog.findViewById(R.id.tv_dialog_ok).setOnClickListener(onClickListener);
            }
            dialog.setText(R.id.tv_dialog_message,"确定要删除 "+ getItem(deletePosition).getName()+" 吗？");
            dialog.show();
        }else if(view.getId() == R.id.btn_update){
            getItem(position).setName("新名字");
            notifyItemChanged(absListView,position);
        }
    }
}
